﻿using log4net;
using Matrix.MaKaYunShang.BLL.WxInterfaceFunc;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.MatrixCloudApi;

namespace Matrix.MaKaYunShang.WeChat.CardProccess
{
    public abstract class BaseWechatCardProcess
    {
        protected readonly WxInterCardFunc WxInterCardFuncObj = new WxInterCardFunc();
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(BaseWechatCardProcess));

        /// <summary>
        /// 卡券通过审核 处理
        /// </summary>
        /// <param name="eventHistory"></param>
        /// <param name="cardStatus"></param>
        public abstract void CardAuditEvent(MxApiWechatEventHistory eventHistory, WechatCommonCardStatus cardStatus);

        /// <summary>
        /// 用户领取卡券
        /// </summary>
        /// <param name="eventHistory"></param>
        public abstract void CustomerGetCardEvent(MxApiWechatEventHistory eventHistory);

        /// <summary>
        /// 用户删除卡券
        /// </summary>
        /// <param name="eventHistory"></param>
        public abstract void UserDeleteCardEvent(MxApiWechatEventHistory eventHistory);

        /// <summary>
        /// 获取卡券信息，若本地无则从微信端重新获取一次，再保存到本地
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="wechatInfo"></param>
        /// <returns></returns>
        protected WeChatCommonCardResult GetCardInfoByCardIdFromWechat(string cardId, WechatInfo wechatInfo)
        {
            var strAccessToken = WxInterCardFuncObj.GetWxToken(wechatInfo.AppId, wechatInfo.Secret, wechatInfo.WechatId);

            _fileLog.Error("LuckMpCard | CardAuditEvent - No CardInfo in database");
            var cardInfoFromWx = WxInterCardFuncObj.QueryCardInfoById(strAccessToken, cardId);
            if (cardInfoFromWx == null || string.IsNullOrWhiteSpace(cardInfoFromWx.card_id))
            {
                _fileLog.Error(
                    "BaseWechatCardProcess | GetCardInfoByCardIdFromWechat | Cannot get the card information from Wechat service. CardId - " +
                    cardId);
                return null;
            }

            return cardInfoFromWx;
        }

    }
}